package com.softlook.mt.server.db.internal;

import java.util.List;
import java.util.Hashtable;
import java.util.Iterator;
import com.softlook.mt.server.db.dto.Client;
import com.softlook.mt.server.db.dto.User;
import com.softlook.mt.server.db.dto.Userdevice;
import com.softlook.mt.server.db.dto.Device;
import com.softlook.mt.server.db.dto.Devicegroup;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.LinkedList;
import com.softlook.mt.server.db.results.usermanager.GetUserDataResult;
import jp.crossfire.framework.database.ISelectResultListener;

public class UserManagerGetUserDataListner implements ISelectResultListener {
	public List result = new LinkedList();
	public Hashtable tables = new Hashtable();
	public List clientTmpCache = new LinkedList();
	public List userTmpCache = new LinkedList();
	public List userdeviceTmpCache = new LinkedList();
	public List deviceTmpCache = new LinkedList();
	public List devicegroupTmpCache = new LinkedList();
	public  UserManagerGetUserDataListner() {
		Hashtable hash = null;
		hash = new Hashtable();
		this.tables.put("client", hash);
		((Hashtable)this.tables.get("client")).put("clientid", new Hashtable());
		hash = new Hashtable();
		this.tables.put("user", hash);
		((Hashtable)this.tables.get("user")).put("userid", new Hashtable());
		((Hashtable)this.tables.get("user")).put("clientid", new Hashtable());
		hash = new Hashtable();
		this.tables.put("userdevice", hash);
		((Hashtable)this.tables.get("userdevice")).put("userid", new Hashtable());
		((Hashtable)this.tables.get("userdevice")).put("deviceid", new Hashtable());
		hash = new Hashtable();
		this.tables.put("device", hash);
		((Hashtable)this.tables.get("device")).put("deviceid", new Hashtable());
		((Hashtable)this.tables.get("device")).put("clientid", new Hashtable());
		((Hashtable)this.tables.get("device")).put("devicegroupid", new Hashtable());
		hash = new Hashtable();
		this.tables.put("devicegroup", hash);
		((Hashtable)this.tables.get("devicegroup")).put("devicegroupid", new Hashtable());
	}
	public void orMap() {
		Iterator it = this.result.iterator();
		while(it.hasNext()){
			// For each result record
			GetUserDataResult data = (GetUserDataResult)it.next();
			Client client = (Client)data.getClient();
			if(client != null && !this.clientTmpCache.contains(client)){
				this.clientTmpCache.add(client);
			}
			User user = (User)data.getUser();
			if(user != null && !this.userTmpCache.contains(user)){
				this.userTmpCache.add(user);
			}
			Userdevice userdevice = (Userdevice)data.getUserdevice();
			if(userdevice != null && !this.userdeviceTmpCache.contains(userdevice)){
				this.userdeviceTmpCache.add(userdevice);
			}
			Device device = (Device)data.getDevice();
			if(device != null && !this.deviceTmpCache.contains(device)){
				this.deviceTmpCache.add(device);
			}
			Devicegroup devicegroup = (Devicegroup)data.getDevicegroup();
			if(devicegroup != null && !this.devicegroupTmpCache.contains(devicegroup)){
				this.devicegroupTmpCache.add(devicegroup);
			}
		}
		// CREATE FOREIGN KEY VALUE HASH
		it = this.clientTmpCache.iterator();
		while(it.hasNext()){
			Client table = (Client)it.next();
			Hashtable hash = null;
			List list = null;
			hash = (Hashtable)this.tables.get("client");
			hash = (Hashtable)hash.get("clientid");
			if(table.getClientid() == null){ continue;}
			list = (List)hash.get(table.getClientid());
			if(list == null){
				list = new LinkedList();
			}
			list.add(table);
			hash.put(table.getClientid(), list);
		}
		it = this.userTmpCache.iterator();
		while(it.hasNext()){
			User table = (User)it.next();
			Hashtable hash = null;
			List list = null;
			hash = (Hashtable)this.tables.get("user");
			hash = (Hashtable)hash.get("userid");
			if(table.getUserid() == null){ continue;}
			list = (List)hash.get(table.getUserid());
			if(list == null){
				list = new LinkedList();
			}
			list.add(table);
			hash.put(table.getUserid(), list);
			hash = (Hashtable)this.tables.get("user");
			hash = (Hashtable)hash.get("clientid");
			if(table.getClientid() == null){ continue;}
			list = (List)hash.get(table.getClientid());
			if(list == null){
				list = new LinkedList();
			}
			list.add(table);
			hash.put(table.getClientid(), list);
		}
		it = this.userdeviceTmpCache.iterator();
		while(it.hasNext()){
			Userdevice table = (Userdevice)it.next();
			Hashtable hash = null;
			List list = null;
			hash = (Hashtable)this.tables.get("userdevice");
			hash = (Hashtable)hash.get("userid");
			if(table.getUserid() == null){ continue;}
			list = (List)hash.get(table.getUserid());
			if(list == null){
				list = new LinkedList();
			}
			list.add(table);
			hash.put(table.getUserid(), list);
			hash = (Hashtable)this.tables.get("userdevice");
			hash = (Hashtable)hash.get("deviceid");
			if(table.getDeviceid() == null){ continue;}
			list = (List)hash.get(table.getDeviceid());
			if(list == null){
				list = new LinkedList();
			}
			list.add(table);
			hash.put(table.getDeviceid(), list);
		}
		it = this.deviceTmpCache.iterator();
		while(it.hasNext()){
			Device table = (Device)it.next();
			Hashtable hash = null;
			List list = null;
			hash = (Hashtable)this.tables.get("device");
			hash = (Hashtable)hash.get("deviceid");
			if(table.getDeviceid() == null){ continue;}
			list = (List)hash.get(table.getDeviceid());
			if(list == null){
				list = new LinkedList();
			}
			list.add(table);
			hash.put(table.getDeviceid(), list);
			hash = (Hashtable)this.tables.get("device");
			hash = (Hashtable)hash.get("clientid");
			if(table.getClientid() == null){ continue;}
			list = (List)hash.get(table.getClientid());
			if(list == null){
				list = new LinkedList();
			}
			list.add(table);
			hash.put(table.getClientid(), list);
			hash = (Hashtable)this.tables.get("device");
			hash = (Hashtable)hash.get("devicegroupid");
			if(table.getDevicegroupid() == null){ continue;}
			list = (List)hash.get(table.getDevicegroupid());
			if(list == null){
				list = new LinkedList();
			}
			list.add(table);
			hash.put(table.getDevicegroupid(), list);
		}
		it = this.devicegroupTmpCache.iterator();
		while(it.hasNext()){
			Devicegroup table = (Devicegroup)it.next();
			Hashtable hash = null;
			List list = null;
			hash = (Hashtable)this.tables.get("devicegroup");
			hash = (Hashtable)hash.get("devicegroupid");
			if(table.getDevicegroupid() == null){ continue;}
			list = (List)hash.get(table.getDevicegroupid());
			if(list == null){
				list = new LinkedList();
			}
			list.add(table);
			hash.put(table.getDevicegroupid(), list);
		}
		// Setup references
		it = this.clientTmpCache.iterator();
		while(it.hasNext()){
			Client table = (Client)it.next();
		 	if(table.getClientid() != null){
				if(((Hashtable)((Hashtable)this.tables.get("user")).get("clientid")).get(table.getClientid()) != null){
					table.setClientidUserRefList((List)((Hashtable)((Hashtable)this.tables.get("user")).get("clientid")).get(table.getClientid()));
				}
			}
		 	if(table.getClientid() != null){
				if(((Hashtable)((Hashtable)this.tables.get("device")).get("clientid")).get(table.getClientid()) != null){
					table.setClientidDeviceRefList((List)((Hashtable)((Hashtable)this.tables.get("device")).get("clientid")).get(table.getClientid()));
				}
			}
		}
		it = this.userTmpCache.iterator();
		while(it.hasNext()){
			User table = (User)it.next();
		 	if(table.getUserid() != null){
				if(((Hashtable)((Hashtable)this.tables.get("userdevice")).get("userid")).get(table.getUserid()) != null){
					table.setUseridUserdeviceRefList((List)((Hashtable)((Hashtable)this.tables.get("userdevice")).get("userid")).get(table.getUserid()));
				}
			}
		 	if(table.getClientid() != null){
				List clientidList = (List)((Hashtable)((Hashtable)this.tables.get("client")).get("clientid")).get(table.getClientid());
				if(clientidList != null && clientidList.size() > 0){
					table.setClientidClientRefObj((Client)clientidList.get(0));
				}
			}
		}
		it = this.userdeviceTmpCache.iterator();
		while(it.hasNext()){
			Userdevice table = (Userdevice)it.next();
		 	if(table.getUserid() != null){
				List useridList = (List)((Hashtable)((Hashtable)this.tables.get("user")).get("userid")).get(table.getUserid());
				if(useridList != null && useridList.size() > 0){
					table.setUseridUserRefObj((User)useridList.get(0));
				}
			}
		 	if(table.getDeviceid() != null){
				List deviceidList = (List)((Hashtable)((Hashtable)this.tables.get("device")).get("deviceid")).get(table.getDeviceid());
				if(deviceidList != null && deviceidList.size() > 0){
					table.setDeviceidDeviceRefObj((Device)deviceidList.get(0));
				}
			}
		}
		it = this.deviceTmpCache.iterator();
		while(it.hasNext()){
			Device table = (Device)it.next();
		 	if(table.getDeviceid() != null){
				if(((Hashtable)((Hashtable)this.tables.get("userdevice")).get("deviceid")).get(table.getDeviceid()) != null){
					table.setDeviceidUserdeviceRefList((List)((Hashtable)((Hashtable)this.tables.get("userdevice")).get("deviceid")).get(table.getDeviceid()));
				}
			}
		 	if(table.getClientid() != null){
				List clientidList = (List)((Hashtable)((Hashtable)this.tables.get("client")).get("clientid")).get(table.getClientid());
				if(clientidList != null && clientidList.size() > 0){
					table.setClientidClientRefObj((Client)clientidList.get(0));
				}
			}
		 	if(table.getDevicegroupid() != null){
				List devicegroupidList = (List)((Hashtable)((Hashtable)this.tables.get("devicegroup")).get("devicegroupid")).get(table.getDevicegroupid());
				if(devicegroupidList != null && devicegroupidList.size() > 0){
					table.setDevicegroupidDevicegroupRefObj((Devicegroup)devicegroupidList.get(0));
				}
			}
		}
		it = this.devicegroupTmpCache.iterator();
		while(it.hasNext()){
			Devicegroup table = (Devicegroup)it.next();
		 	if(table.getDevicegroupid() != null){
				if(((Hashtable)((Hashtable)this.tables.get("device")).get("devicegroupid")).get(table.getDevicegroupid()) != null){
					table.setDevicegroupidDeviceRefList((List)((Hashtable)((Hashtable)this.tables.get("device")).get("devicegroupid")).get(table.getDevicegroupid()));
				}
			}
		}
	}
	public void getResult(ResultSet result, ResultSetMetaData meta) throws Exception {
		int maxCols = meta.getColumnCount();
		List tableColSettedList= new LinkedList();
		GetUserDataResult data = new GetUserDataResult();
		for(int i = 0; i < maxCols; i++){
			if(meta.getColumnName(i+1) == null){continue;}
		
			if(result.getObject(i+1) != null){
				data.put(meta.getColumnName(i+1).toUpperCase(), result.getObject(i+1));
			}
			String columnName = meta.getColumnName(i+1);
			if(Client.hasColumn(columnName)){
				if(columnName.toUpperCase().equals("clientid".toUpperCase())
		                   && !tableColSettedList.contains("client.clientid")){
					tableColSettedList.add("client.clientid");
					if(result.getObject(i+1) != null){
						data.getClientWithNew().setClientid(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("name".toUpperCase())
		                   && !tableColSettedList.contains("client.name")){
					tableColSettedList.add("client.name");
					if(result.getObject(i+1) != null){
						data.getClientWithNew().setName(new java.lang.String(result.getString(i+1)));
					}
					continue;
				}
			}
			if(User.hasColumn(columnName)){
				if(columnName.toUpperCase().equals("userid".toUpperCase())
		                   && !tableColSettedList.contains("user.userid")){
					tableColSettedList.add("user.userid");
					if(result.getObject(i+1) != null){
						data.getUserWithNew().setUserid(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("clientid".toUpperCase())
		                   && !tableColSettedList.contains("user.clientid")){
					tableColSettedList.add("user.clientid");
					if(result.getObject(i+1) != null){
						data.getUserWithNew().setClientid(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("login".toUpperCase())
		                   && !tableColSettedList.contains("user.login")){
					tableColSettedList.add("user.login");
					if(result.getObject(i+1) != null){
						data.getUserWithNew().setLogin(new java.lang.String(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("password".toUpperCase())
		                   && !tableColSettedList.contains("user.password")){
					tableColSettedList.add("user.password");
					if(result.getObject(i+1) != null){
						data.getUserWithNew().setPassword(new java.lang.String(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("name".toUpperCase())
		                   && !tableColSettedList.contains("user.name")){
					tableColSettedList.add("user.name");
					if(result.getObject(i+1) != null){
						data.getUserWithNew().setName(new java.lang.String(result.getString(i+1)));
					}
					continue;
				}
			}
			if(Userdevice.hasColumn(columnName)){
				if(columnName.toUpperCase().equals("userid".toUpperCase())
		                   && !tableColSettedList.contains("userdevice.userid")){
					tableColSettedList.add("userdevice.userid");
					if(result.getObject(i+1) != null){
						data.getUserdeviceWithNew().setUserid(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("deviceid".toUpperCase())
		                   && !tableColSettedList.contains("userdevice.deviceid")){
					tableColSettedList.add("userdevice.deviceid");
					if(result.getObject(i+1) != null){
						data.getUserdeviceWithNew().setDeviceid(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("isactive".toUpperCase())
		                   && !tableColSettedList.contains("userdevice.isactive")){
					tableColSettedList.add("userdevice.isactive");
					if(result.getObject(i+1) != null){
						data.getUserdeviceWithNew().setIsactive(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
			}
			if(Device.hasColumn(columnName)){
				if(columnName.toUpperCase().equals("deviceid".toUpperCase())
		                   && !tableColSettedList.contains("device.deviceid")){
					tableColSettedList.add("device.deviceid");
					if(result.getObject(i+1) != null){
						data.getDeviceWithNew().setDeviceid(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("imei".toUpperCase())
		                   && !tableColSettedList.contains("device.imei")){
					tableColSettedList.add("device.imei");
					if(result.getObject(i+1) != null){
						data.getDeviceWithNew().setImei(new java.lang.String(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("clientid".toUpperCase())
		                   && !tableColSettedList.contains("device.clientid")){
					tableColSettedList.add("device.clientid");
					if(result.getObject(i+1) != null){
						data.getDeviceWithNew().setClientid(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("devicegroupid".toUpperCase())
		                   && !tableColSettedList.contains("device.devicegroupid")){
					tableColSettedList.add("device.devicegroupid");
					if(result.getObject(i+1) != null){
						data.getDeviceWithNew().setDevicegroupid(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("name".toUpperCase())
		                   && !tableColSettedList.contains("device.name")){
					tableColSettedList.add("device.name");
					if(result.getObject(i+1) != null){
						data.getDeviceWithNew().setName(new java.lang.String(result.getString(i+1)));
					}
					continue;
				}
			}
			if(Devicegroup.hasColumn(columnName)){
				if(columnName.toUpperCase().equals("devicegroupid".toUpperCase())
		                   && !tableColSettedList.contains("devicegroup.devicegroupid")){
					tableColSettedList.add("devicegroup.devicegroupid");
					if(result.getObject(i+1) != null){
						data.getDevicegroupWithNew().setDevicegroupid(new java.lang.Integer(result.getString(i+1)));
					}
					continue;
				}
				if(columnName.toUpperCase().equals("name".toUpperCase())
		                   && !tableColSettedList.contains("devicegroup.name")){
					tableColSettedList.add("devicegroup.name");
					if(result.getObject(i+1) != null){
						data.getDevicegroupWithNew().setName(new java.lang.String(result.getString(i+1)));
					}
					continue;
				}
			}
		}
		tableColSettedList.clear();
		this.result.add(data);
	}
}
